<template>
  <div class="product-card" @click="viewProduct(product.id)">
    <img :src="product.image" :alt="product.name" class="product-image">
    <div class="product-info">
      <h3 class="product-name" v-text="product.name"></h3>
      <p class="product-category" v-text="product.category"></p>
      <div class="product-rating">
        <span class="rating">★ <span v-text="product.rating"></span></span>
        <span class="reviews">(<span v-text="product.reviews"></span>条评论)</span>
      </div>
      <div class="product-price">
        <span class="current-price">¥<span v-text="product.price"></span></span>
        <span class="original-price" v-if="product.originalPrice">¥<span v-text="product.originalPrice"></span></span>
      </div>
      <button @click.stop="addToCart" class="add-to-cart-btn">加入购物车</button>
    </div>
  </div>
</template>

<script>
import { useRouter } from 'vue-router'

export default {
  name: 'ProductCard',
  props: {
    product: {
      type: Object,
      required: true
    }
  },
  emits: ['view-product'],
  setup(props, { emit }) {
    const router = useRouter()
    
    const viewProduct = (id) => {
      emit('view-product', id)
    }
    
    const addToCart = () => {
      console.log(`商品 ${props.product.name} 已添加到购物车`)
      // 这里可以调用购物车添加方法
    }
    
    return {
      viewProduct,
      addToCart
    }
  }
}
</script>

<style scoped>
.product-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  overflow: hidden;
  transition: transform 0.3s, box-shadow 0.3s;
  cursor: pointer;
}

.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
}

.product-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.product-info {
  padding: 1rem;
}

.product-name {
  font-size: 1.1rem;
  margin-bottom: 0.5rem;
  color: #333;
}

.product-category {
  font-size: 0.9rem;
  color: #666;
  margin-bottom: 0.5rem;
}

.product-rating {
  display: flex;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}

.rating {
  color: #ffc107;
}

.reviews {
  font-size: 0.8rem;
  color: #666;
}

.product-price {
  display: flex;
  align-items: center;
  margin-bottom: 1rem;
}

.current-price {
  font-size: 1.2rem;
  font-weight: bold;
  color: #e91e63;
  margin-right: 0.5rem;
}

.original-price {
  font-size: 0.9rem;
  color: #999;
  text-decoration: line-through;
}

.add-to-cart-btn {
  width: 100%;
  background-color: #42b983;
  color: white;
  border: none;
  padding: 0.8rem;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.add-to-cart-btn:hover {
  background-color: #359c6d;
}
</style>